/*
 * Copyright 2018-10-14 21:39:18 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.MaterielDetailAO;
import com.lap.scp.erp.domain.MaterielDetailDO;
import com.lap.scp.erp.query.MaterielDetailQueryVO;

/**
 * <pre>
 *  物料明细明细信息控制对象，提供 物料明细明细信息 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-14 21:39:18
 * @since 2.0.0
 */
@Controller
@RequestMapping("materielDetail")
public class MaterielDetailController {
	/**
	 * 物料明细明细信息应用流程对象
	 */
	@Resource
	private MaterielDetailAO materielDetailAO;

	/**
	 * <pre>
	 * 进入新增物料明细明细信息
	 * </pre>
	 * 
	 * @return
	 */
	@GetMapping(value = "add")
	public ModelAndView add(Integer materielId) {
		ModelAndView andView = new ModelAndView("erp/materielDetailIn");
		Resp<MaterielDetailDO> resp = materielDetailAO.queryForAdd(materielId);
		if (resp.isSuccess()) {
			MaterielDetailDO detailDO = resp.getData();
			detailDO.setMaterielId(materielId);
			andView.addObject("materielDetailDO", detailDO);
		} else {
			MaterielDetailDO detailDO = new MaterielDetailDO();
			detailDO.setMaterielId(materielId);
			andView.addObject("materielDetailDO", detailDO);
		}
		return andView;
	}

	/**
	 * <pre>
	 * 新增保存物料明细明细信息
	 * </pre>
	 * 
	 * @param materielDetailDO
	 * @return
	 */
	@PostMapping(value = "add")
	@ResponseBody
	public Resp<MaterielDetailDO> add(MaterielDetailDO materielDetailDO) {
		return materielDetailAO.insertMaterielDetail(materielDetailDO);
	}

	/**
	 * <pre>
	 * 根据ID删除物料明细明细信息
	 * // TODO 生成的代码，如果确认你已经使用该方法，请删除该行注释，不然后续清理代码的时候，将会删除该方法
	 * </pre>
	 * 
	 * @param materielDetailId
	 * @param map
	 * @param attributes
	 * @return
	 */
	@GetMapping(value = "delete/{materielDetailId}")
	public ModelAndView deleteById(@PathVariable Integer materielDetailId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = materielDetailAO.deleteById(materielDetailId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/materielDetail/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据条件、分页信息查询物料明细明细信息
	 *  // TODO 生成的代码，如果确认你已经使用该方法，请删除该行注释，不然后续清理代码的时候，将会删除该方法
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, MaterielDetailQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<MaterielDetailDO> pr = materielDetailAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("erp/materielDetailList");
			andView.addObject("queryVO", queryVO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入调整库存页面
	 * </pre>
	 * 
	 * @param materielDetailId
	 * @return
	 */
	@GetMapping(value = "editQuantity/{materielDetailId}")
	public ModelAndView editQuantity(@PathVariable Integer materielDetailId) {
		ModelAndView andView = new ModelAndView();

		Resp<MaterielDetailDO> resp = materielDetailAO.queryById(materielDetailId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/materielDetailEdit");
			andView.addObject("materielDetailDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入库存入库界面
	 * </pre>
	 * 
	 * @param materielDetailId
	 * @return
	 */
	@GetMapping(value = "editIn/{materielDetailId}")
	public ModelAndView editIn(@PathVariable Integer materielDetailId) {
		ModelAndView andView = new ModelAndView();

		Resp<MaterielDetailDO> resp = materielDetailAO.queryById(materielDetailId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/materielDetailIn");
			andView.addObject("materielDetailDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入物料明细明细信息 编辑页面
	 * </pre>
	 * 
	 * @param materielDetailId
	 * @return
	 */
	@GetMapping(value = "editOut/{materielDetailId}")
	public ModelAndView editOut(@PathVariable Integer materielDetailId) {
		ModelAndView andView = new ModelAndView();

		Resp<MaterielDetailDO> resp = materielDetailAO.queryById(materielDetailId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/materielDetailOut");
			andView.addObject("materielDetailDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 库存调整
	 * </pre>
	 * 
	 * @param materielDetailDO
	 * @return
	 */
	@PostMapping(value = "editQuantity")
	@ResponseBody
	public Resp<Integer> editQuantity(MaterielDetailDO materielDetailDO) {
		return materielDetailAO.updateQuantity(materielDetailDO);
	}

	/**
	 * <pre>
	 * 物料入库
	 * </pre>
	 * 
	 * @param materielDetailDO
	 * @return
	 */
	@PostMapping(value = "editIn")
	@ResponseBody
	public Resp<Integer> editIn(MaterielDetailDO materielDetailDO) {
		return materielDetailAO.updateIn(materielDetailDO);
	}

	/**
	 * <pre>
	 * 物料入库
	 * </pre>
	 * 
	 * @param materielDetailDO
	 * @return
	 */
	@PostMapping(value = "editOut")
	@ResponseBody
	public Resp<Integer> updateOut(MaterielDetailDO materielDetailDO, @RequestParam Integer contractId) {
		return materielDetailAO.updateOut(materielDetailDO, contractId);
	}

}
